<template>

  <el-dialog title="审核备件" :visible.sync="dialogFormVisible" :modal-append-to-body="false">
    <el-form :rules="rules" ref="dataForm" :model="temp" label-position="right" label-width="20%"
             style='width: 90%; margin-left:1%;'>
      <el-form-item label="父级" prop="">
        <span class="fontShow2">{{temp.show}}</span>
      </el-form-item>
      <el-form-item label="备件名称" prop="partName">
        <el-input v-model="temp.partName"></el-input>
      </el-form-item>
      <el-form-item label="备件属性" prop="partSourceType">
        <el-radio-group v-model="temp.partSourceType" size="mini">
          <el-radio v-for="(value, key) in _PartSourceType" :key="key" :label="key">{{value}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备件类型" prop="partType">
        <el-radio-group v-model="temp.partType" size="mini">
          <el-radio v-for="(value, key) in _PartType" :key="key" :label="key">{{value}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="采购方式" prop="purchaseType">
        <el-radio-group v-model="temp.purchaseType" size="mini">
          <el-radio v-for="(value, key) in _PurchaseType" :key="key" :label="key">{{value}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="规格型号" prop="specType">
        <el-input v-model="temp.specType"></el-input>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input v-model="temp.unit"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input :autosize="{ minRows: 1, maxRows: 3}" v-model="temp.remark" type="textarea" placeholder="备注"/>
      </el-form-item>
      <el-form-item v-for="(attr, i) in temp.attrList"
                    :label="'扩展属性' + i" :key="attr.key" :prop="'attrList.' + i + '.attrName'">
        <el-col :span="8">
          <el-input v-model="attr.attrName" placeholder="属性名"></el-input>
        </el-col>
        <el-col :span="8">
          <el-input v-model="attr.attrVal" placeholder="属性值"></el-input>
        </el-col>
        <el-col :span="6">
          &nbsp;&nbsp;<el-button @click.prevent="addAttr()" size="mini" type="success"><i class="el-icon-plus"></i>
        </el-button>
          <el-button @click.prevent="removeAttrs(attr)" size="mini" type="danger"><i class="el-icon-delete"></i>
          </el-button>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <template v-if="nodeId!='myHiTask' ">
        <el-button type="warning" @click="updateSubmit(0)" id="cfmbtm0">保存</el-button>
        <el-button type="danger" @click="updateSubmit(2)" id="cfmbtm2">不通过</el-button>
        <el-button type="primary" @click="updateSubmit(1)" id="cfmbtm1">审核通过</el-button>
      </template>
      <el-button @click="dialogFormVisible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>

  import {updateOneNoId} from '@/api/common'
  import {disableBtn, ERRS, SUCS} from '@/utils'
  import mixinCode from './mixinCode'

  export default {
    mixins: [mixinCode],
    name: 'auditEdit',
    props: {
      // levStatus: Number,
      treeNodeData: Object,
      nodeId: String,
    },
    data: function () {
      return {
        dialogFormVisible: false,
        dialogStatus: '',
        temp: {},
        applyType: 0,  //发起方式 0直接添加 1发起申请
        rules: {
          partName: [
            {required: true, message: '请输入备件名', trigger: 'blur'},
            {min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur'}
          ],

        }
      }
    },

    methods: {

      // + - 属性项
      removeAttrs(item) {
        var index = this.temp.attrList.indexOf(item)
        if (index !== -1) {
          this.temp.attrList.splice(index, 1)
        }
      },
      addAttr() {
        this.temp.attrList.push({attrName: '', attrVal: ''});
      },
      //审修改
      audit(row) {
        console.log(row);
        console.log(this.nodeId);
        this.temp = this.nodeId == this.GLB.start_node ? eval('(' + row.opFormJson + ')') : row.hiParams;

        this.temp.id = row._id
        /*        this.temp = {
                  id: row._id,
                  codeEndId: exp.parent._id,
                  iconUrl: row.iconUrl,
                  partName: row.partName,   //备件名称
                  partSourceType: row.partSourceType, //备件属性 PURCHASE, RANDOM
                  partType: row.partType,       //备件类型 故障备件、一般备件 FAULT, ORDINARY
                  purchaseType: row.purchaseType,       //“采购方式”包含：零采、统购、委托采购、寄售
                  remark: row.remark,
                  specType: row.specType,   // 规格型号
                  unit: row.unit,   // 单位
                  attrList: row.partAttrList, //其它属性
                };*/
        // this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })

      },
      doSubmit(url, type) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let btn = type ? type>1?'cfmbtm2' :'cfmbtm1' : 'cfmbtm0'
            disableBtn(btn);
            updateOneNoId(url, this.temp).then(res => {
              if (res.code === 0) {
                let msg = type ? '审核成功!' : '保存成功'
                SUCS(msg)
                this.$emit('childGetList')
              } else {
                ERRS(res.message)
              }
            })
          }
        })
      },
      updateSubmit(type) {
        let tp = this.temp
        let ruTaskUserId = tp.id
        tp.audit = type===1 ? 1 : 0  //传1 通过 其它 不通过
        delete tp.id
        this.dialogFormVisible = false
        this.temp = {
          opFormJson: JSON.stringify(tp),
          submit: type ? true : false  //提交?保存
        }
        this.doSubmit(`flowable/ru/${ruTaskUserId}/saveOrSubmit`, type)
      },

    }
  }
</script>

